﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace RG.Common.WPF.BaseSelector
{

    /// <summary>
    /// Interaction logic for BaseSelector.xaml
    /// </summary>
    public partial class BaseSelector : UserControl
    {
        public BaseSelector()
        {
            InitializeComponent();
        }
        #region bool IsSelected
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(BaseSelector),
                                            new FrameworkPropertyMetadata(false));

        public bool IsSelected
        {
            set { SetValue(IsSelectedProperty, value); }
            get { return (bool)GetValue(IsSelectedProperty); }
        }
        #endregion


        #region EnterFrame SelectedItem

        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(INamed), typeof(BaseSelector),
                                            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnSelectedChanged)));



        public INamed SelectedItem
        {
            set { SetValue(SelectedItemProperty, value); }
            get { return (INamed)GetValue(SelectedItemProperty); }
        }
        #endregion

        #region string ItemName
        public static readonly DependencyProperty ItemNameProperty =
            DependencyProperty.Register("ItemName", typeof(string), typeof(BaseSelector),
                                            new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnItemNameChanged)));


        public string ItemName
        {
            set { SetValue(ItemNameProperty, value); }
            get { return (string)GetValue(ItemNameProperty); }
        }
        #endregion

        #region IUserHelper Helper
        public static readonly DependencyProperty HelperProperty =
            DependencyProperty.Register("Helper", typeof(IHelper), typeof(BaseSelector),
                                            new FrameworkPropertyMetadata(null));

        public IHelper Helper
        {
            set { SetValue(HelperProperty, value); }
            get { return (IHelper)GetValue(HelperProperty); }
        }
        #endregion


        private static void OnSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var accessPoint = (INamed)e.NewValue;
            d.SetValue(ItemNameProperty, (accessPoint == null) ? "" : accessPoint.Name);
            d.SetValue(IsSelectedProperty, accessPoint != null);
        }


        private static void OnItemNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var accessPoint = (INamed)d.GetValue(SelectedItemProperty);
            var oldName = (accessPoint == null) ? "" : accessPoint.Name;
            d.SetValue(IsSelectedProperty, oldName == (string)e.NewValue);
        }

        private void bSearch_Click(object sender, RoutedEventArgs e)
        {
            if (Helper == null) return;
            var new_value = Helper.Search(ItemName);
            if (new_value != null)
                SelectedItem = new_value;
        }

        private void bOverView_Click(object sender, RoutedEventArgs e)
        {

            if (Helper == null) return;

            var new_value = Helper.Overview();
            if (new_value != null)
                SelectedItem = new_value;
        }

        private void bClear_Click(object sender, RoutedEventArgs e)
        {
            SelectedItem = null;
        }


        private void tItemName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Return) return;
            bSearch_Click(sender, new RoutedEventArgs(e.RoutedEvent));
            e.Handled = true;
        }
    }
}
